#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMimeData>
#include <QFileInfo>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setAcceptDrops(true);
    ui->plainTextEdit->setAcceptDrops(false);
    ui->label->setAcceptDrops(true);
    ui->label->setScaledContents(true);//自动缩放

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::resizeEvent(QResizeEvent *event)
{
    QSize size=ui->plainTextEdit->size();
    ui->plainTextEdit->move(10,10);
    ui->label->move(10,size.height()+10);
    ui->plainTextEdit->resize(this->width()-20,size.height());
    ui->label->resize(this->width()-20,this->height()-size.height()-30);
    event->accept();
}

void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
    qDebug()<<"dragEnterEvent";
    qDebug()<<"item:event->mimeData()->formats()";
    for(auto& item:event->mimeData()->formats())
    {
        qDebug()<<item;
    }
    qDebug()<<"event->mimeData()->urls()";
    qDebug()<<event->mimeData()->urls();

    if(event->mimeData()->hasUrls())
    {
        QString fileName = event->mimeData()->urls().at(0).fileName();//完整的文件名包含拓展名
        QFileInfo fileInfo(fileName);
        QString extension = fileInfo.suffix().toLower();//文件拓展名，转换成小写方便之后处理
        if(extension == "jpg")
        {
            event->acceptProposedAction();//接收动作

        }
        else
        {
            event->ignore();
        }
    }
    else
    {
        event->ignore();
    }
}

void MainWindow::dropEvent(QDropEvent *event)
{
    QString filePath = event->mimeData()->urls().at(0).path();//文件路径，它是linux格式/那种
    //注意，这里最左面多了个'/'，请看urls的调试信息

    qDebug()<<filePath;
    filePath = filePath.right(filePath.length()-1);//去掉左面的'/'
    QPixmap pixmap(filePath);
    ui->label->setPixmap(pixmap);
    event->accept();

}





















